package com.rub.prajna.core.video

import com.rub.prajna.core.AvExecutor
import com.rub.prajna.core.AvObject.Companion.ffmpegCommand
import com.rub.prajna.core.utils.FileKit
import com.sun.jna.Platform
import kotlinx.coroutines.*
import java.io.File
import kotlin.properties.Delegates


class VideoHandler {

    private lateinit var outPutFile: String
    private lateinit var inputFile: String
    private lateinit var fileType: String
    private lateinit var startTime: String
    private var cutTime by Delegates.notNull<Int>()
    private var status: Boolean = false

    constructor(inputFile: String) {
        this.inputFile = inputFile
    }

    constructor(inputFile: String, fileType: String, cutTime: Int) {
        this.inputFile = inputFile
        this.fileType = fileType
        this.cutTime = cutTime
    }

    constructor(inputFile: String, cutTime: Int, startTime: String) {
        this.inputFile = inputFile
        this.cutTime = cutTime
        this.startTime = startTime
    }


    fun getInputFile(): String = this.inputFile
    fun getOutPutFile(): String = this.outPutFile
    fun getStatus(): Boolean = this.status


    /**
     * ffmpeg -ss 00:00:10 -i 111.mp4 -t 502 -codec copy -y cut_1.mp4
     * ffmpeg -ss 00:00:15 -t 00:00:05 -i input.mp4 -vcodec copy -acodec copy output.mp4
     * @param startTime
     * @param endTime
     */
    fun cutVideo(startTime: String, endTime: String): VideoHandler {

        val newFilePath = createFilePath()


        //输出文件包含目录
        val outFile = newFilePath + File(this.inputFile).name
        val commands: MutableList<String> = ArrayList()
        commands.add(ffmpegCommand)
        commands.add("-ss")
        commands.add(startTime)
        commands.add("-t")
        commands.add(endTime)
        commands.add("-i")
        commands.add(this.inputFile)
        commands.add("-vcodec")
        commands.add("copy")
        commands.add("-acodec")
        commands.add("copy")
        commands.add("-y")
        commands.add(outFile)
        GlobalScope.launch {
            val rest = withContext(Dispatchers.IO) {
                AvExecutor().runCommand(commands)
            }
            status = rest
        }
        this.outPutFile = outFile
        return this
    }

    /**
     * ffmpeg -i 1.mp4 -vframes 1 -ss 0 -y 1.jpg
     */
    fun cutImage(): VideoHandler {

        val newFilePath = createFilePath()

        val fileName = File(this.inputFile).name
        //获取文件名称，不包含类型
        val newFileName: String = fileName.substring(0, fileName.lastIndexOf("."))

        //输出文件包含目录
        val outFile = "$newFilePath$newFileName.${this.fileType}"
        val commands: MutableList<String> = ArrayList()
        commands.add(ffmpegCommand)
        commands.add("-i")
        commands.add(this.inputFile)
        commands.add("-vframes")
        commands.add("1")
        commands.add("-ss")
        commands.add(this.cutTime.toString())
        commands.add("-y")
        commands.add(outFile)

        GlobalScope.launch{
            val rest = withContext(Dispatchers.IO) {
                AvExecutor().runCommand(commands)
            }
            status = rest
        }

        this.outPutFile = outFile
        return this
    }

    /**
     * kotlin携程执行命令
     */
    @OptIn(DelicateCoroutinesApi::class)
    private suspend fun suspendCmd(commands: MutableList<String>) {
        GlobalScope.launch {
            val rest = withContext(Dispatchers.IO) {
                AvExecutor().runCommand(commands)
            }
            status = rest
        }
    }

    /**
     * ffmpeg -ss 00:00:10 -t 3 -i input.mp4 -y output.gif
     */
    fun videoConvertGif(): VideoHandler {

        val newFilePath = createFilePath()

        val fileName = File(this.inputFile).name
        //获取文件名称，不包含类型
        val newFileName: String = fileName.substring(0, fileName.lastIndexOf("."))

        //输出文件包含目录
        val outFile = "$newFilePath$newFileName.gif"
        val commands: MutableList<String> = ArrayList()
        commands.add(ffmpegCommand)
        commands.add("-ss")
        commands.add(this.startTime)
        commands.add("-t")
        commands.add(this.cutTime.toString())
        commands.add("-i")
        commands.add(this.inputFile)
        commands.add("-y")
        commands.add(outFile)
        GlobalScope.launch {
            val rest = withContext(Dispatchers.IO) {
                AvExecutor().runCommand(commands)
            }
            status = rest
        }
        this.outPutFile = outFile
        return this
    }

    /**
     * 创建文件路径
     * folder
     */
    private fun createFilePath(): String {
        val file = File(this.inputFile)
        val filePath = file.absolutePath.replace(file.name, "")
        //新路径
        var newFilePath = ""
        if (Platform.isWindows()) {
            newFilePath = filePath + "output\\"
        } else if (Platform.isMac() || Platform.isLinux() || Platform.iskFreeBSD()) {
            newFilePath = filePath + "output/"
        }
        //创建文件夹
        FileKit().mkDirs(newFilePath)

        return newFilePath

    }
}
